package org.tuoren.official.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.tuoren.common.core.utils.MapstructUtils;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.tuoren.official.domain.bo.HonorsBo;
import org.tuoren.official.domain.vo.HonorsVo;
import org.tuoren.official.domain.Honors;
import org.tuoren.official.mapper.HonorsMapper;
import org.tuoren.official.mapper.HonorsTypeMapper;
import org.tuoren.official.service.IHonorsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 荣誉资质Service业务层处理
 *
 * @author wl
 * @date 2024-11-26
 */
@RequiredArgsConstructor
@Service
public class HonorsServiceImpl implements IHonorsService {

    private final HonorsMapper baseMapper;
    private final HonorsTypeMapper typeMapper;

    /**
     * 查询荣誉资质
     *
     * @param id 主键
     * @return 荣誉资质
     */
    @Override
    public HonorsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询荣誉资质列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 荣誉资质分页列表
     */
    @Override
    public TableDataInfo<HonorsVo> queryPageList(HonorsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Honors> lqw = buildQueryWrapper(bo);
        Page<HonorsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(r -> r.setTypeName(typeMapper.selectVoById(r.getTypeId()).getName()));
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的荣誉资质列表
     *
     * @param bo 查询条件
     * @return 荣誉资质列表
     */
    @Override
    public List<HonorsVo> queryList(HonorsBo bo) {
        LambdaQueryWrapper<Honors> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Honors> buildQueryWrapper(HonorsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Honors> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTypeId() != null, Honors::getTypeId, bo.getTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoUrl()), Honors::getPhotoUrl, bo.getPhotoUrl());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Honors::getName, bo.getName());
        lqw.orderByAsc(Honors::getOrderNum);
        return lqw;
    }

    /**
     * 新增荣誉资质
     *
     * @param bo 荣誉资质
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HonorsBo bo) {
        Honors add = MapstructUtils.convert(bo, Honors.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改荣誉资质
     *
     * @param bo 荣誉资质
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HonorsBo bo) {
        Honors update = MapstructUtils.convert(bo, Honors.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Honors entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除荣誉资质信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
